// 19/04/2009 Dennis
class TracksForm extends Form
{
  ListBox listbox;
  
  // 19/04/2009 Dennis
  public TracksForm()
  {
    this.bgcolor = color(255);
    
    // Create listbox
    listbox = new ListBox();
    listbox.x = 0;
    listbox.y = 0;
    listbox.width = width;
    listbox.height = height;
    listbox.header.add(new ListBoxHeader("Track Title", 40));
    listbox.header.add(new ListBoxHeader("Artist", 20));
    listbox.header.add(new ListBoxHeader("Album", 20));
    listbox.header.add(new ListBoxHeader("Popularity", 20));
    addControl(listbox);
    
    title = "Select a track";
  }
  
  // 19/04/2009 Dennis
  public void processEvent(Control c)
  {
    if(c == listbox)
    {
      String lbl = (String)listbox.getSelection().labels.get(0);
      String artist = (String)listbox.getSelection().labels.get(1);
      label = lbl;
      db.qryTrack = db.getTrack(artist+lbl);
      db.qryIntervals = 50;
      app.addForm(app.graphForm);
    }
  }
  
  // 19/04/2009 Dennis, Bagdat
  public void setActive()
  {
    listbox.scrollpos = 0;
    listbox.items.clear();
    ArrayList tracks = db.queryTracks();
    Iterator i = tracks.iterator();
    int maxBarVal = 0;
    int minBarVal = 9999999;
    while(i.hasNext())
    {
      Track track = (Track)i.next();
      int popularity = track.getPopularity(db.qryUser != null ? db.qryUser.name : null, db.qryMin, db.qryMax);
      if(popularity > maxBarVal)
        maxBarVal = popularity;
      if(popularity < minBarVal)
        minBarVal = popularity;
      ListBoxItem lbi = new ListBoxItem();
      lbi.labels.add(track.name);
      lbi.labels.add(track.artist.name);
      lbi.labels.add(track.album.name);
      lbi.labels.add(popularity);
      listbox.items.add(lbi);
    }
    listbox.maxBarVal = maxBarVal;
    listbox.minBarVal = minBarVal;
    listbox.sortPopularity();
  }
}
